﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tetris.Game.View
{
    public partial class Form1 : Form
    {
        private List<RainDrop> rainDrops;
        private Random random;
        private Font font;
        private Brush brush;
        public Form1()
        {
            InitializeComponent();
            SetStyle(
               ControlStyles.ResizeRedraw
               | ControlStyles.DoubleBuffer
               | ControlStyles.UserPaint
               | ControlStyles.AllPaintingInWmPaint
               | ControlStyles.SupportsTransparentBackColor,
               true
               );

            rainDrops = new List<RainDrop>();
            random = new Random();
            font = new Font("Consolas", 12);
            brush = new SolidBrush(Color.Green);
            timer1.Interval = 50;
            timer1.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Invalidate();
            UpdateRainDrops();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            foreach (var drop in rainDrops)
            {
                using (Brush brush = new SolidBrush(drop.Color))
                {
                    e.Graphics.DrawString(drop.Text, font, brush, drop.X, drop.Y);
                }
            }
        }
        private void UpdateRainDrops()
        {
            for (int i = 0; i < 10; i++)
            {
                rainDrops.Add(new RainDrop
                {
                    X = random.Next(this.Width),
                    Y = 0,
                    Speed = random.Next(5, 20),
                    Text = ((char)random.Next(33, 126)).ToString(), // example string
                    Color = Color.FromArgb(255, Color.Green)
                });
            }

            for (int i = rainDrops.Count - 1; i >= 0; i--)
            {
                var drop = rainDrops[i];
                drop.Y += drop.Speed;
                int alpha = drop.Color.A - 3;
                if (alpha < 0) alpha = 0;
                drop.Color = Color.FromArgb(alpha, drop.Color.R, drop.Color.G, drop.Color.B);

                if (drop.Y > this.Height || drop.Color.A == 0)
                {
                    rainDrops.RemoveAt(i);
                }
            }
        }
    }
    class RainDrop
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int Speed { get; set; }
        public string Text { get; set; }
        public Color Color { get; set; }
    }
}
